package plugins.schoolcard;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.BillList;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * 标准单据列表插件
 */
public class StatusListLock extends AbstractListPlugin implements Plugin {
    // 按钮标识
    private static final String BTN_LOCK_KEY = "dcb4_lock";
    private static final String BTN_UNLOCK_KEY = "dcb4_unlock";
    // 状态字段标识
    private static final String STATUS_FIELD = "dcb4_status";
    // 列表控件标识
    private static final String BILL_LIST_KEY = "billlistap";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 注册Lock和Unlock按钮点击事件
        this.addItemClickListeners(BTN_LOCK_KEY, BTN_UNLOCK_KEY);
    }

    // 新增单据关闭回调
    public void billClosedCallBack(ClosedCallBackEvent event) {
        if (event.getActionId().equals("view") || event.getActionId().equals("edit")) {
            this.getView().invokeOperation("refresh");
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        String itemKey = evt.getItemKey();

        if (BTN_LOCK_KEY.equals(itemKey)) {
            updateSelectedRowsStatus("C", "挂失");
        }
        else if (BTN_UNLOCK_KEY.equals(itemKey)) {
            updateSelectedRowsStatus("B", "激活");
        }
    }

    /**
     * 更新选中行的状态
     * @param newStatus 新状态值 (C=锁定, B=解锁)
     * @param actionName 操作名称（用于提示信息）
     */
    private void updateSelectedRowsStatus(String newStatus, String actionName) {


        // 获取列表控件
        BillList billList = (BillList) this.getView().getControl(BILL_LIST_KEY);
        String billFormId = billList.getBillFormId();

        // 获取选中行
        ListSelectedRowCollection selectedRows = billList.getSelectedRows();
        if (selectedRows == null || selectedRows.isEmpty()) {
            this.getView().showTipNotification("请选择要" + actionName + "的数据行");
            return;
        }

        // 获取选中行主键
        List<Object> selectedIds = new ArrayList<>();
        for (ListSelectedRow row : selectedRows) {
            selectedIds.add(row.getPrimaryKeyValue());
        }

        // 批量加载数据
        DynamicObject[] dataObjects = BusinessDataServiceHelper.load(
                billFormId,
                "id," + STATUS_FIELD,
                new QFilter[]{new QFilter("id", QCP.in, selectedIds.toArray())}
        );

        if (dataObjects != null && dataObjects.length > 0) {
            for (DynamicObject obj : dataObjects) {
                obj.set(STATUS_FIELD, newStatus);
            }

            // 批量保存
            SaveServiceHelper.save(dataObjects);
            this.getView().showSuccessNotification(actionName + "操作成功");

            // 刷新列表
            this.getView().invokeOperation("refresh");
        }
    }
}
